#include "head.h"
#include <string.h>
void *request_handler(void *arg)
{
	int msgid;
	struct user_info user; 
	struct linkmsg linkmsg;
	struct msg_queue msg_buf;
	struct storage_info sto;
	msgid = msg_init();
	printf("***************request_handler*****************\n");
	while(1)
	{
		if(msgrcv(msgid, &msg_buf, sizeof(msg_buf)-sizeof(long), 1, 0) == -1)
		{
			perror("fail to msgrcv");
		}
		switch(msg_buf.msgtype)
		{
			case C_LOGIN:
				memset(&linkmsg,'\0' ,sizeof(linkmsg));
				linkmsg.type = msg_buf.msgtype;
				printf("linkmsg.type = %ld\n", linkmsg.type);
				memcpy(linkmsg.text, msg_buf.text, sizeof(linkmsg.text));
				pthread_mutex_lock(&mutex.mutex_link_db);
				input_linkqueue(&linkmsg, link_db);
				pthread_mutex_unlock(&mutex.mutex_link_db);
				pthread_cond_broadcast(&cond.cond_sqlite);
				printf("data is recv login ok\n");
				break;
			case C_ENV:
				memset(&linkmsg,'\0' ,sizeof(linkmsg));
				linkmsg.type = msg_buf.msgtype;
				memcpy(linkmsg.text, msg_buf.text, sizeof(linkmsg.text));
				memcpy(&sto, msg_buf.text, sizeof(sto));
				pthread_mutex_lock(&mutex.mutex_link_db);
				input_linkqueue(&linkmsg, link_db);
				pthread_mutex_unlock(&mutex.mutex_link_db);
				pthread_cond_broadcast(&cond.cond_sqlite);
				pthread_mutex_lock(&mutex.mutex_global);
				all_storage_data.storage_dev[sto.s_house_no].s_threshold = sto.s_threshold;
				pthread_mutex_unlock(&mutex.mutex_global);
				pthread_cond_broadcast(&cond.cond_refresh);
				printf("data is recv env  ok\n");
				break;
			case C_CMD:
				memset(&linkmsg, '\0', sizeof(linkmsg));
				linkmsg.type = 1;
				memcpy(linkmsg.text, msg_buf.text, sizeof(linkmsg.text));
				pthread_mutex_lock(&mutex.mutex_link_cmd);
				input_linkqueue(&linkmsg, link_cmd);
				pthread_mutex_unlock(&mutex.mutex_link_cmd);
				pthread_cond_broadcast(&cond.cond_zigbee_send);
				printf("data is cmd ok\n");

				break;
			default:break;
		}
	}
}
